import Vue from "vue";
import axios from "axios";
import VueAxios from "vue-axios";
import { getToken, removeToken, saveToken } from "./token";
import { Toast } from "vant";
import router from "../router";

Vue.use(VueAxios, axios);
const instance = axios.create({
    baseURL: "http://localhost:1337",
    timeout: 10000
  });

  instance.interceptors.request.use(
    function(config) {
      //如果用户有登录，就自动为axios请求添加token（请求头）
      if (getToken()) {
        config.headers.Authorization = "Bearer " + getToken().token;
      }
      return config;
    },
    function(error) {
      // 对请求错误做些什么
      return Promise.reject(error);
    }
  );
  

  export default instance;

  function loginCode(mobile) {
    return instance({
      url: "/au/code",
      method: "post",
      data: {
        mobile,
      }
    });
  }
  export { loginCode };


  function userLogin({ mobile, code }) {
    return instance({
      url: "/au/login",
      method: "post",
      data: { mobile, code }
    });
  }
  
  export { userLogin };

